// 获取日历事件
var updata = {};
var nowevent;
var loginer = App.get('loginer');
var Plan_done = {};
$.ajax({
	url: "/api/complex/MyCalendar",
	type: "get",
	data: {
		"sid": loginer.sid
	},
	success: function(result) {
		App.set("$Cache_Calendar_event", result.result);
		App.set("$Cache_Calendar_type.total", result.length);
		Plan_done.total = result.length;
		App.set("$Cache_Calendar_type.done", result.done);
		Plan_done.done = result.done;
		App.set("$Cache_Calendar_type.percent", ((result.done / result.length) * 100).toFixed(2) + "%");
		console.log(result.result, result);
	},
	error: function(e) {
		console.log(e);
	}
});

// 修改日历
App.set("$Event.change_plan", function(e, vi) {
	var init_done = vi.get('color') == 0 ? 1 : -1;
	var str = vi.get('color') == 0 ? '点击确定将此计划标注为已完成？' : '点击确定将此计划标注为未完成？';
	myConfirm(str, function() {
		// console.log(vi.get('color') == 0 ? '#4688f7' : '0')
		$.ajax({
			url: "/api/complex/MyCalendar",
			type: "put",
			cache: false,
			data: data = {
				id: vi.get('id'),
				color: vi.get('color') == 0 ? '#4688f7' : '0'
			},
			// sycn: true,
			success: function(result) {
				alert('状态更改成功！！！');
				Plan_done.done += init_done;
				App.set("$Cache_Calendar_type.done", Plan_done.done);
				App.set("$Cache_Calendar_type.percent", ((Plan_done.done / Plan_done.total) * 100).toFixed(2) + "%");
				vi.set("color", data.color);
				// console.log(result);
			},
			error: function(e) {
				console.log(e);
			}
		})
	});
});

// 删除
App.set("$Event.delete_plan", function(e, vi) {
	myConfirm("确定删除这条计划吗？", function() {
		console.log(vi.get("id"))
		$.ajax({
			url: "/api/complex/MyCalendar",
			type: "delete",
			data: {
				"id": vi.get("id")
			},
			success: function(result) {
				myConfirm("计划删除成功！！！", function() {
					// location.reload();
					vi.remove();
				}, function() {});
				// console.log(result);
			},
			error: function(e) {
				console.log(e);
			}
		}, function() {})
	});
})
// console.log(event);
// 添加日历事件
App.set("$Event.submit_plan", function() {
	var start = $("[name='start']").val();
	var end = $("[name='end']").val();
	if ((end && start > end) || !start) {
		alert("日期选择有误！！！");
		return;
	} else {
		var data = {
			start: start,
			end: end + " 24:00:00" || start
		};
		var title = App.get("$Cache_plan_input");
		if (!title) {
			alert("请标注好事件！！！");
			return
		} else {
			data.title = title;
			data.color = 0;
			data.allDay = "true";
			var url = App.get("$Cache_plan_website_url");
			if (url) {
				data.url = url;
			};
			data.id = new Date().getTime();
			data.sid = loginer.sid;
			// console.log(data);
			$.ajax({
				url: "/api/complex/MyCalendar",
				type: "post",
				// dataType: "json",
				data: data,
				success: function(result) {
					// console.log(result);
					$("input").val("");
					alert("事件添加成功！！！");
					var $Cache_Calendar_event = App.get("$Cache_Calendar_event");
					$Cache_Calendar_event.unshift(data);
					App.set("$Cache_Calendar_event", $Cache_Calendar_event);
					Plan_done.total += 1;
					App.set("$Cache_Calendar_type.total", Plan_done.total);
					App.set("$Cache_Calendar_type.percent", ((Plan_done.done / Plan_done.total) * 100).toFixed(2) + "%");
					// location.reload();
				},
				error: function(e) {
					console.log(e);
				}
			})
		};
	};
});

// datepicker
$('#datetimepicker').datetimepicker({
	format: 'yyyy-MM-dd',
	language: 'en',
	pickDate: true,
	// pickTime: true,
	// hourStep: 1,
	// minuteStep: 15,
	// secondStep: 30,
	inputMask: true
});
$('#end').datetimepicker({
	format: 'yyyy-MM-dd',
	language: 'en',
	pickDate: true,
	// pickTime: true,
	// hourStep: 1,
	// minuteStep: 15,
	// secondStep: 30,
	inputMask: true
});


App.set("$Event.add_new_plan", function() {
	App.model.toggle("$Cache_isAdd_plan");
})